package util;

import java.util.*;

import static util.Regex.splitByRegex;

/**
 * @fileName: AcademicianRegex
 * @author: Han
 * @date: 2018/5/21 15:30
 * @dscription: 解析字符串
 */
public class AcademicianRegex {

    /**
     * 解析页面,获取院士名称和其对应url
     *
     * @param pageStr 页面
     * @return Map
     */
    public static List<String> getAcademicianNameUrlList(String pageStr) {

        List<String> list = new ArrayList<>();
        //分成单独的行
        String[] lines = pageStr.split("\n");
        String department = "-";
        for (String line : lines) {
            int departmentStartFlag = line.indexOf("、");
            int departmentEndFlag = line.indexOf("部(");
            if (departmentStartFlag != -1 && departmentEndFlag != -1) {
                department = line.substring(departmentStartFlag + 1, departmentEndFlag + 1);
            }
            try {
                boolean isExist = line.contains("<li class=\"name_list\">");
                if (isExist) {
                    //url和名字之间的字符串
                    int flag = line.indexOf("\" target=\"_blank\">");
                    //解析姓名
                    String name = line.substring(flag + 18, line.length() - 9);
                    //解析url
                    String url = line.substring(line.indexOf("<a href=\"") + 9, flag);
                    url = "http://www.cae.cn" + url;
                    //放入容器
                    list.add(name + "：" + url + "：" + department);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return list;
    }

    public static Map<String, String> getEdu1(String pageStr) {

        Map<String, String> academicianMap = new HashMap<>(8);

        String[] lines = pageStr.split("\n");

        boolean hasFlag = false;
        String descStr = "";
        //找出简介
        for (String line : lines) {
            if (hasFlag) {
                descStr = line;
                break;
            }
            if (line.contains("intro")) {
                hasFlag = true;
            }
        }
        String[] descArr = descStr.split("。");
        //解析姓名
        String nameStr = descStr.substring(0, descStr.indexOf("（"));
        nameStr = nameStr.substring(nameStr.lastIndexOf(";") + 1);
        academicianMap.put("姓名", nameStr);
        //解析学历,本科:0,研究生:1
        int educationBackground = 0;
        //院校解析
        for (String desc : descArr) {
            try {
                if (desc.contains("毕业于")) {
                    int collegeEndFlag = desc.indexOf("大学") + 2;
                    collegeEndFlag = collegeEndFlag == -1 ? desc.indexOf("学院") + 2 : collegeEndFlag;
                    int collegeStartFlag = desc.indexOf("毕业于") + 3;
                    //本科毕业院校
                    String regularSchool = desc.substring(collegeStartFlag, collegeEndFlag);
                    academicianMap.put("本科毕业院校", regularSchool);
                    //本科专业
                    String majorStr = "-";
                    if (desc.contains("专业")) {
                        int majorEndFlag = desc.indexOf("专业") + 2;
                        majorStr = desc.substring(collegeEndFlag, majorEndFlag);
                    }
                    academicianMap.put("本科专业", majorStr);
                    if (desc.contains("硕士")) {
                        academicianMap.put("硕士毕业院校", regularSchool);
                        academicianMap.put("硕士专业", majorStr);
                        educationBackground = 1;
                    }
                } else if (desc.contains("博士学位")) {
                    int dcStartFlag = desc.indexOf("获") + 1;
                    int dcEndFlag = desc.indexOf("大学") + 2;
                    dcEndFlag = dcEndFlag == -1 ? desc.indexOf("学院") + 2 : dcEndFlag;
                    String dcSchool = desc.substring(dcStartFlag, dcEndFlag);
                    academicianMap.put("博士毕业院校", dcSchool);
                    String majorStr = "-";
                    if (desc.contains("专业")) {
                        int majorEndFlag = desc.indexOf("专业") + 2;
                        majorStr = desc.substring(dcEndFlag, majorEndFlag);
                    }
                    academicianMap.put("博士专业", majorStr);
                    educationBackground = 1;
                }
            } catch (Exception e) {
                System.out.println("出错:" + descStr);
            }
        }
        String educationBackgroundStr = "本科";
        if (educationBackground == 1) {
            educationBackgroundStr = "研究生";
        }
        academicianMap.put("学历", educationBackgroundStr);

        return academicianMap;
    }

    /**
     * 获取文字描述
     *
     * @param pageStr 整个页面
     * @return 描述
     */
    public static String getDesc(String pageStr) {

        String[] lines = pageStr.split("\n");

        boolean hasFlag = false;
        String descStr = "";
        //找出简介
        for (String line : lines) {
            if (hasFlag) {
                descStr = line;
                break;
            }
            if (line.contains("intro")) {
                hasFlag = true;
            }
        }
        return descStr;
    }

    public static Map<String, String> getEdu(String descStr, String name, String url, String department) {

        Map<String, String> academicianMap = new LinkedHashMap<>(10);
        academicianMap.put("学部", department);
        academicianMap.put("姓名", name);
        academicianMap.put("学历", "-");
        academicianMap.put("本科毕业院校", "-");
        academicianMap.put("本科专业", "-");
        academicianMap.put("硕士毕业院校", "-");
        academicianMap.put("硕士专业", "-");
        academicianMap.put("博士毕业院校", "-");
        academicianMap.put("博士专业", "-");
        academicianMap.put("URL", url);
        academicianMap.put("简介", descStr);

        String[] descArr = descStr.split("\n");
        List<String> descList = splitByRegex(descArr[0], "[，,。.;；:：]");
        //学历
        String educationBackground = "-";
        for (String desc : descList) {
            try {
                if (desc.contains("本科")) {
                    educationBackground = "本科";
                    //本科毕业院校
                    int schoolStartFlag = desc.indexOf("于");
                    schoolStartFlag = schoolStartFlag == -1 ? desc.indexOf("在") :
                            schoolStartFlag;
                    schoolStartFlag = schoolStartFlag == -1 ? desc.indexOf("年") :
                            schoolStartFlag;
                    String tempStr = desc.substring(0, schoolStartFlag);
                    if (tempStr.contains("大学") || tempStr.contains("学院")) {
                        schoolStartFlag = desc.indexOf("年");
                    }
                    int schoolEndFlag = -1;
                    tempStr = desc;
                    int tempCount = 0;
                    //解析"学"的前一个字符和后一个字符是否符合要求
                    while (tempStr.contains("学")) {
                        int tempFlag = tempStr.indexOf("学");
                        char nextChar = 0;
                        if (tempFlag < tempStr.length() - 1) {
                            nextChar = tempStr.charAt(tempFlag + 1);
                        }
                        char lastChar = 0;
                        if (tempFlag > 0) {
                            lastChar = tempStr.charAt(tempFlag - 1);
                        }
                        if (nextChar == '院') {
                            tempFlag += tempCount;
                            schoolEndFlag = tempFlag + 2;
                            break;
                        } else if (lastChar == '大') {
                            tempFlag += tempCount;
                            schoolEndFlag = tempFlag + 1;
                            break;
                        } else {
                            schoolEndFlag = -1;
                            tempCount += tempFlag + 1;
                        }
                        tempStr = tempStr.substring(tempFlag + 1);
                    }
                    String school;
                    if (schoolStartFlag != -1 && schoolEndFlag != -1 && schoolEndFlag > schoolStartFlag) {
                        schoolStartFlag += 1;
                        school = desc.substring(schoolStartFlag, schoolEndFlag);
                    } else {
                        school = desc;
                    }
                    academicianMap.put("本科毕业院校", school);
                    //本科专业
                    int majorFlag = desc.indexOf("专业");
                    majorFlag = majorFlag == -1 ? desc.indexOf("系") : majorFlag;
                    String major = "-";
                    if (majorFlag != -1 && schoolEndFlag != -1 && schoolEndFlag < majorFlag) {
                        major = desc.substring(schoolEndFlag, majorFlag);
                    }
                    academicianMap.put("本科专业", major);
                } else if (desc.contains("研究生")) {
                    //研究生毕业院校
                    int schoolStartFlag = desc.indexOf("于");
                    schoolStartFlag = schoolStartFlag == -1 ? desc.indexOf("在") :
                            schoolStartFlag;
                    schoolStartFlag = schoolStartFlag == -1 ? desc.indexOf("年") :
                            schoolStartFlag;
                    String tempStr = desc.substring(0, schoolStartFlag);
                    if (tempStr.contains("大学") || tempStr.contains("学院")) {
                        schoolStartFlag = desc.indexOf("年");
                    }
                    int schoolEndFlag = -1;
                    tempStr = desc;
                    int tempCount = 0;
                    //解析"学"的前一个字符和后一个字符是否符合要求
                    while (tempStr.contains("学")) {
                        int tempFlag = tempStr.indexOf("学");
                        char nextChar = 0;
                        if (tempFlag < tempStr.length() - 1) {
                            nextChar = tempStr.charAt(tempFlag + 1);
                        }
                        char lastChar = 0;
                        if (tempFlag > 0) {
                            lastChar = tempStr.charAt(tempFlag - 1);
                        }
                        if (nextChar == '院') {
                            tempFlag += tempCount;
                            schoolEndFlag = tempFlag + 2;
                            break;
                        } else if (lastChar == '大') {
                            tempFlag += tempCount;
                            schoolEndFlag = tempFlag + 1;
                            break;
                        } else {
                            schoolEndFlag = -1;
                            tempCount += tempFlag + 1;
                        }
                        tempStr = tempStr.substring(tempFlag + 1);
                    }
                    String school;
                    if (schoolStartFlag != -1 && schoolEndFlag != -1 && schoolEndFlag > schoolStartFlag) {
                        schoolStartFlag += 1;
                        school = desc.substring(schoolStartFlag, schoolEndFlag);
                    } else {
                        school = desc;
                    }
                    //研究生专业
                    int majorFlag = desc.indexOf("专业");
                    majorFlag = majorFlag == -1 ? desc.indexOf("系") : majorFlag;
                    String major = "-";
                    if (majorFlag != -1 && schoolEndFlag != -1 && schoolEndFlag < majorFlag) {
                        major = desc.substring(schoolEndFlag, majorFlag);
                    }
                    educationBackground = "研究生";
                    if (desc.contains("博士")) {
                        academicianMap.put("博士毕业院校", school);
                        academicianMap.put("博士专业", major);
                    } else {
                        academicianMap.put("硕士毕业院校", school);
                        academicianMap.put("硕士专业", major);
                    }
                } else if (desc.contains("毕业")) {
                    educationBackground = "本科";
                    //本科毕业院校
                    int schoolStartFlag = desc.indexOf("于");
                    int tempFlag = desc.indexOf("在");
                    //获取两个中较小的那个
                    if (schoolStartFlag != -1 && tempFlag != -1) {
                        schoolStartFlag = schoolStartFlag < tempFlag ? schoolStartFlag : tempFlag;
                    }
                    //如果两个都不存在,则用"年"的位置代替
                    schoolStartFlag = schoolStartFlag == -1 ? desc.indexOf("年") :
                            schoolStartFlag;
                    String tempStr = desc.substring(0, schoolStartFlag);
                    if (tempStr.contains("大学") || tempStr.contains("学院")) {
                        schoolStartFlag = desc.indexOf("年");
                    }
                    int schoolEndFlag = -1;
                    tempStr = desc;
                    int tempCount = 0;
                    //解析"学"的前一个字符和后一个字符是否符合要求
                    while (tempStr.contains("学")) {
                        tempFlag = tempStr.indexOf("学");
                        char nextChar = 0;
                        if (tempFlag < tempStr.length() - 1) {
                            nextChar = tempStr.charAt(tempFlag + 1);
                        }
                        char lastChar = 0;
                        if (tempFlag > 0) {
                            lastChar = tempStr.charAt(tempFlag - 1);
                        }
                        if (nextChar == '院') {
                            tempFlag += tempCount;
                            schoolEndFlag = tempFlag + 2;
                            break;
                        } else if (lastChar == '大') {
                            tempFlag += tempCount;
                            schoolEndFlag = tempFlag + 1;
                            break;
                        } else {
                            schoolEndFlag = -1;
                            tempCount += tempFlag + 1;
                        }
                        tempStr = tempStr.substring(tempFlag + 1);
                    }
                    String school;
                    if (schoolStartFlag != -1 && schoolEndFlag != -1 && schoolEndFlag > schoolStartFlag) {
                        schoolStartFlag += 1;
                        school = desc.substring(schoolStartFlag, schoolEndFlag);
                    } else {
                        school = desc;
                    }
                    if (desc.contains("硕士")) {
                        academicianMap.put("硕士毕业院校", school);
                    } else if (desc.contains("博士")) {
                        academicianMap.put("博士毕业院校", school);
                    } else {
                        academicianMap.put("本科毕业院校", school);
                    }
                    //本科专业
                    int majorFlag = desc.indexOf("专业");
                    majorFlag = majorFlag == -1 ? desc.indexOf("系") : majorFlag;
                    String major = "-";
                    if (majorFlag != -1 && schoolEndFlag <= majorFlag) {
                        major = desc.substring(schoolEndFlag, majorFlag);
                    }
                    if (desc.contains("硕士")) {
                        academicianMap.put("硕士专业", major);
                    } else if (desc.contains("博士")) {
                        academicianMap.put("博士专业", major);
                    } else {
                        academicianMap.put("本科专业", major);
                    }
                } else if (desc.contains("硕士学位")) {
                    educationBackground = "研究生";
                    //硕士专业
                    int schoolStartFlag = desc.indexOf("获");
                    int tempFlag = desc.indexOf("在");
                    //获取两个中较小的那个
                    if (schoolStartFlag != -1 && tempFlag != -1) {
                        schoolStartFlag = schoolStartFlag < tempFlag ? schoolStartFlag : tempFlag;
                    }
                    //如果两个都不存在,则用"年"的位置代替
                    schoolStartFlag = schoolStartFlag == -1 ? desc.indexOf("年") :
                            schoolStartFlag;
                    String tempStr = desc.substring(0, schoolStartFlag);
                    if (tempStr.contains("大学") || tempStr.contains("学院")) {
                        schoolStartFlag = desc.indexOf("年");
                    }
                    int schoolEndFlag = -1;
                    tempStr = desc;
                    int tempCount = 0;
                    //解析"学"的前一个字符和后一个字符是否符合要求
                    while (tempStr.contains("学")) {
                        tempFlag = tempStr.indexOf("学");
                        char nextChar = 0;
                        if (tempFlag < tempStr.length() - 1) {
                            nextChar = tempStr.charAt(tempFlag + 1);
                        }
                        char lastChar = 0;
                        if (tempFlag > 0) {
                            lastChar = tempStr.charAt(tempFlag - 1);
                        }
                        if (nextChar == '院') {
                            tempFlag += tempCount;
                            schoolEndFlag = tempFlag + 2;
                            break;
                        } else if (lastChar == '大') {
                            tempFlag += tempCount;
                            schoolEndFlag = tempFlag + 1;
                            break;
                        } else {
                            schoolEndFlag = -1;
                            tempCount += tempFlag + 1;
                        }
                        tempStr = tempStr.substring(tempFlag + 1);
                    }
                    String school = "-";
                    if (schoolStartFlag != -1 && schoolEndFlag != -1 && schoolEndFlag > schoolStartFlag) {
                        school = desc.substring(schoolStartFlag + 1, schoolEndFlag);
                    }
                    //如果没有获得硕士毕业院校,则用本科代替
                    if ("-".equals(school)) {
                        school = academicianMap.get("本科毕业院校");
                    }
                    academicianMap.put("硕士毕业院校", school);
                    //博士专业
                    int majorFlag = desc.indexOf("专业");
                    majorFlag = majorFlag == -1 ? desc.indexOf("系") + 1 :
                            majorFlag + 2;
                    String major = "-";
                    if (desc.contains("专业") || desc.contains("系")) {
                        if (majorFlag < schoolEndFlag || schoolEndFlag == -1) {
                            schoolEndFlag = schoolStartFlag + 1;
                        }
                        major = desc.substring(schoolEndFlag, majorFlag);
                    }
                    //如果没有获得硕士专业,则用本科专业代替
                    if ("-".equals(major)) {
                        major = academicianMap.get("本科专业");
                    }
                    academicianMap.put("硕士专业", major);

                } else if (desc.contains("博士学位")) {
                    educationBackground = "研究生";
                    //博士专业
                    int schoolStartFlag = desc.indexOf("获");
                    int tempFlag = desc.indexOf("在");
                    //获取两个中较小的那个
                    if (schoolStartFlag != -1 && tempFlag != -1) {
                        schoolStartFlag = schoolStartFlag < tempFlag ? schoolStartFlag : tempFlag;
                    }
                    //如果两个都不存在,则用"年"的位置代替
                    schoolStartFlag = schoolStartFlag == -1 ? desc.indexOf("年") :
                            schoolStartFlag;
                    String tempStr = desc.substring(0, schoolStartFlag);
                    if (tempStr.contains("大学") || tempStr.contains("学院")) {
                        schoolStartFlag = desc.indexOf("年");
                    }
                    int schoolEndFlag = -1;
                    tempStr = desc;
                    int tempCount = 0;
                    //解析"学"的前一个字符和后一个字符是否符合要求
                    while (tempStr.contains("学")) {
                        tempFlag = tempStr.indexOf("学");
                        char nextChar = 0;
                        if (tempFlag < tempStr.length() - 1) {
                            nextChar = tempStr.charAt(tempFlag + 1);
                        }
                        char lastChar = 0;
                        if (tempFlag > 0) {
                            lastChar = tempStr.charAt(tempFlag - 1);
                        }
                        if (nextChar == '院') {
                            tempFlag += tempCount;
                            schoolEndFlag = tempFlag + 2;
                            break;
                        } else if (lastChar == '大') {
                            tempFlag += tempCount;
                            schoolEndFlag = tempFlag + 1;
                            break;
                        } else {
                            schoolEndFlag = -1;
                            tempCount += tempFlag + 1;
                        }
                        tempStr = tempStr.substring(tempFlag + 1);
                    }
                    String school = "-";
                    if (schoolStartFlag != -1 && schoolEndFlag != -1 && schoolEndFlag > schoolStartFlag) {
                        school = desc.substring(schoolStartFlag + 1, schoolEndFlag);
                    }
                    if ("-".equals(school)) {
                        school = academicianMap.get("本科毕业院校");
                    }
                    academicianMap.put("博士毕业院校", school);
                    //博士专业
                    int majorFlag = desc.indexOf("专业");
                    majorFlag = majorFlag == -1 ? desc.indexOf("系") + 1 :
                            majorFlag + 2;
                    String major = "-";
                    if (desc.contains("专业") || desc.contains("系")) {
                        if (majorFlag < schoolEndFlag || schoolEndFlag == -1) {
                            schoolEndFlag = schoolStartFlag + 1;
                        }
                        major = desc.substring(schoolEndFlag, majorFlag);
                    }
                    if ("-".equals(major)) {
                        major = academicianMap.get("本科专业");
                    }
                    academicianMap.put("博士专业", major);
                }
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println(name + ":" + desc);
            }
        }
        academicianMap.put("学历", educationBackground);

        return academicianMap;
    }

    public static String toChineseHex(String s) {

        byte[] bt = s.getBytes();

        StringBuilder s1 = new StringBuilder();

        for (byte aBt : bt) {

            String tempStr = Integer.toHexString(aBt);

            if (tempStr.length() > 2) {

                tempStr = tempStr.substring(tempStr.length() - 2);
            }

            s1.append("%").append(tempStr);

        }

        return s1.toString().toUpperCase();
    }
}